package com.qianfeng.goods.service;

import java.util.List;

import com.qianfeng.goods.bean.Goods;
import com.qianfeng.goods.dao.GoodsDao;
import com.qianfeng.utils.DBUtils;

public class GoodsService {
	public List<Goods> getList(Goods goods) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);
		List<Goods> list = goodsDao.getList(goods);
		DBUtils.release();
		return list;
	}

	public int getCount(Goods goods) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);

		int num = goodsDao.getCount(goods);

		DBUtils.release();

		return num;
	}

	/**
	 * 根据id删除分类
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteById(long id) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);

		int num = goodsDao.deleteById(id);

		DBUtils.release();

		return num > 0;
	}

	/**
	 * 根据id查询分类的详细信息
	 * 
	 * @param id
	 * @return
	 */
	public Goods getById(long id) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);

		Goods goods = goodsDao.getById(id);

		DBUtils.release();

		return goods;
	}

	public boolean updateGoods(Goods goods) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);
		int num = 0;
		if (goods.getId() > 0) {
			num = goodsDao.updateById(goods);
		} else {
			goods.setCreate_time(System.currentTimeMillis());
			num = goodsDao.saveGoods(goods);
		}
		DBUtils.release();
		return num > 0;
	}

	public boolean updateStatus(Goods goods) {
		GoodsDao goodsDao = DBUtils.getMapper(GoodsDao.class);
		int num = goodsDao.updateStatus(goods);
		DBUtils.release();
		return num > 0;
	}
}
